package com.hnichr.ump.build7.message.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hnichr.ump.build7.message.MessageArrivedHandle;
import com.hnichr.ump.build7.share.SnapshotShare;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.Date;

@Component
public class SnapshotMessageArrivedHandle implements MessageArrivedHandle {
    private static final Logger logger = LoggerFactory.getLogger(SnapshotMessageArrivedHandle.class);

    @Override
    public boolean support(String topic, String message) {
        return "b956ba98-988cd83e/device/message/up/snapshot".equals(topic);
    }

    @Override
    public void handle(String topic, String message) {
        try{
            JSONObject jsonObject = JSON.parseObject(message);
            JSONObject payload = (JSONObject)jsonObject.get("payload");
            String image64 = payload.get("image_content").toString();
            synchronized (SnapshotShare.getInstance()){
                SnapshotShare.getInstance().setImageContent(image64);
                SnapshotShare.getInstance().notifyAll();
            }

        }
        catch (Exception e){
            logger.error("SnapshotMessageArrivedHandle报错",e);
        }


    }
}
